var Fenzuguanli = {
	init : function () {
		ajax("/teacher-groups/list", {}, callback);
		$("#add-mygroup").click(Fenzuguanli.show_dialog);
		$("#dialog-fenzuguanli").find("button[name=save]").click(Fenzuguanli.addOrUpdate);
		$("#dialog-fenzuguanli").find("button[name=cancel]").click(Fenzuguanli.close_dialog);
		$(".add-mygroup-dl").delegate("dd", "click", Fenzuguanli.select_group);
		$(".mygroup-header").delegate("input[name=bianji]", "click", Fenzuguanli.show_dialog);
		$(".mygroup-header").delegate("input[name=remove_teacher]", "click", Fenzuguanli.remove_teacher);
		$(".mygroup-header").delegate("input[name=jiesan]", "click", Fenzuguanli.remove);
	},
	list : function (result) {
		if (result != null && result.data != null && result.data.teacher_groups.length >= 0) {
    			var content = $(".add-mygroup-dl");
    			content.empty();
    			var teacher_groups = result.data.teacher_groups;
    			var trs = "";
    			for (var i = 0; i < teacher_groups.length; i++) {
    				var group = teacher_groups[i];
    				trs += ("<dd group_id='" + group.id + "'>");
    				trs += group.name;
    				trs += ("</dd>");
    			}
    			content.append(trs);
    			$(".add-mygroup-dl").find("dd:eq(0)").click();
    		}
	},
	select_group : function (obj, page, size) {
		$(this).addClass("cur").siblings().removeClass("cur");
		if (page == undefined) {
			page = 0;
		}
		if (size == undefined) {
			size = 0;
		}
		var group_id = $(this).attr("group_id");
		var data = {
			"page" : page,
			"size" : size,
			"group_id" : group_id
		}
		ajax("/teacher-groups/teachers/list", data, callback_teacher_list);
	},
	teacher_list : function (result) {
		if (result != null && result.data != null && result.data.teachers.length >= 0) {
			var dd = $(".add-mygroup-dl").find("dd[class=cur]");
			$(".mygroup-header").html($(dd).text() + '<span class="num">组员人数：' + result.data.teachers.length + '人</span><a href="fenzuguanli-xinzengzuyuan.html?group_id=' + $(dd).attr("group_id") + '" class="btn-5faee3">新增组员</a><input class="btn-ccc" type="button" name="remove_teacher" value="删除组员">' + 
				'<input class="btn-5faee3 dialogShow" type="button" name="bianji" value="编辑组名"><input class="btn-5faee3" type="button" name="jiesan" value="解散分组">');
    			var content = $(".table-list tbody");
    			content.empty();
    			var teachers = result.data.teachers;
    			var trs = "";
    			for (var i = 0; i < teachers.length; i++) {
    				var userOne = teachers[i];
    				trs += ("<tr>");
    				var id = ("<td><input type='checkbox' name='teacher_id' value='" + userOne.id +"' /></td>");
    				var img = ('<td><img src="../images/man.jpg" width="36" height="36"/></td>');
    				var nick = ("<td>" + (userOne.nick == undefined ? "" : userOne.nick) +"</td>");
    				var genderDesc = "未知";
    				if (userOne.gender == 0) {
    					genderDesc = "未知";
    				} else if (userOne.gender == 1) {
    					genderDesc = "女性";
    				} else if (userOne.gender == 2) {
    					genderDesc = "男性";
    				}
    				var gender = ("<td>" + genderDesc +"</td>");
    				var birthday = ("<td>" + userOne.birthday +"</td>");
    				var mobile = ("<td>" + userOne.mobile +"</td>");
    				var statusDesc = "未知";
    				if (userOne.id_status == 0) {
    					statusDesc = "未认证";
    				} else if (userOne.type == 1) {
    					statusDesc = "已认证";
    				} else if (userOne.type == 2) {
    					statusDesc = "认证失败";
    				}
    				var status = ("<td>" + statusDesc +"</td>");
    				var operate = ('<td><span class="btn-border dialogShow">教练详情</span></td>');
    				trs += ( id + img + nick + gender + birthday + mobile + status + operate);
    				trs += ("</tr>");
    			}
    			content.append(trs);
    		}
	},
	remove_teacher : function () {
		var ids = $(".table-list tbody").find("input[name=teacher_id]:checked");
		if (ids== null || ids.length == 0) {
			return;
		}
		var user_ids = [];
		for( var i=0; i<ids.length; i++) {
			user_ids.push($(ids[i]).val());
		}
		var group_id = $(".add-mygroup-dl").find("dd[class=cur]").attr("group_id");
		var data = {
			"group_id" : group_id,
			"user_ids" : user_ids
		};
		ajax("/teacher-groups/teachers/remove" , data);
	},
	show_dialog : function () {
		if ($(this).attr("id") == "add-mygroup") {
			$("#dialog-fenzuguanli").find("input[name='id']").val(0);
			$("#dialog-fenzuguanli").find("input[name='name']").val("");
		} else {
			var dd = $(".add-mygroup-dl").find("dd[class=cur]");
			$("#dialog-fenzuguanli").find("input[name='id']").val($(dd).attr("group_id"));
			$("#dialog-fenzuguanli").find("input[name='name']").val($(dd).text());
		}
		$("#dialog-fenzuguanli").css({"display" : "block"});
	},
	close_dialog : function() {
		$("#dialog-fenzuguanli").css({"display" : "none"});
	},
	addOrUpdate : function () {
		var id = $("#dialog-fenzuguanli").find("input[name='id']").val();
		var name = $("#dialog-fenzuguanli").find("input[name='name']").val();
		if (name.trim() == "") {
			alert("请输入分组名称");
			return;
		}
		if (id != "" && id > 0) {
			ajax("/teacher-groups/update",{"group_id" : id, "name" : name}, success)
		} else{
			ajax("/teacher-groups/create", {"name" : name}, success);
		}
	},
	remove : function () {
		var group_id = $(".add-mygroup-dl").find("dd[class=cur]").attr("group_id");
		ajax("/teacher-groups/delete", {"group_id" : group_id}, success);
//		var user_ids = [];
//		user_ids.push("1559083823241");
//		var data = {
//			"group_id" : 1741767447694,
//			"user_ids" : user_ids
//		};
//		ajax("/teacher-groups/teachers/add" , data);
	}
}

function callback(result) {
	Fenzuguanli.list(result);
}

function callback_teacher_list(result) {
	Fenzuguanli.teacher_list(result);
}

function callback_teacher_remove_success(result) {
	Fenzuguanli.teacher_remove_success(result);
}

function success(result) {
	if (result != null && result.code == 0) {
		$("#dialog-fenzuguanli").css({"display" : "none"});
		ajax("/teacher-groups/list", {}, callback);
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

$(function(){
	Fenzuguanli.init();
});
